//
//  RightMenuHeaderView.m
//  NOAHWM
//
//  Created by Ryan on 13-5-29.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "RightMenuHeaderView.h"
#import "NYMemberCenterDataManager.h"
#import "MenuDataManager.h"

#define kRankImageMapFileName @"rankIconNameMap"

@implementation RightMenuHeaderView

@synthesize asynImageView,nameLabel,detailLabel;
@synthesize jifengLabel;
@synthesize huiyuanLabel,dengjiImageView;

- (void)reloadData
{
    if ([NYMemberCenterDataManager sharedManager].loginMember.memberType == kMemberAuthentication) {
        self.athuView.hidden = NO;
        self.regView.hidden = YES;
    }
    else {
        self.athuView.hidden = YES;
        self.regView.hidden = NO;
    }
    
    if([[NYMemberCenterDataManager sharedManager].loginMember.is_auth isEqualToString:@"1"])
        self.applyAuthButton.enabled = NO;
    else
        self.applyAuthButton.enabled = YES;
    
    self.nameLabel.text = [NYMemberCenterDataManager sharedManager].loginMember.userName;
    
    NSString *huiyuan = [NYMemberCenterDataManager sharedManager].loginMember.memberCardType;
    if([huiyuan isEqualToString:@"一颗钻"] || [huiyuan isEqualToString:@"两颗钻"] || [huiyuan isEqualToString:@"三颗钻"])
        huiyuan = @"钻石";
    if([huiyuan isEqualToString:@"四颗钻"] || [huiyuan isEqualToString:@"五颗钻"] || [huiyuan isEqualToString:@"六颗钻"])
        huiyuan = @"超钻";
    
    self.huiyuanLabel.text = [NSString stringWithFormat:@"%@会员", huiyuan];
    
    
    self.jifengLabel.text = [NSString stringWithFormat:@"积分: %@",[NYMemberCenterDataManager sharedManager].loginMember.comper_point];
    self.jifengLabel.textColor = [UIColor colorWithRed:245/256.f green:100/256.f blue:35/256.f alpha:1.0];
    NSString *path = [[NSBundle mainBundle] pathForResource:kRankImageMapFileName ofType:@"plist"];
    NSDictionary *rankDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *rankIconName = [rankDict objectForKey:[NYMemberCenterDataManager sharedManager].loginMember.memberCardType];
    self.dengjiImageView.image = [UIImage imageNamed:rankIconName];
    
    self.asynImageView.cacheDir = kUserImagesDir;
    [self.asynImageView aysnLoadImageWithUrl:[NYMemberCenterDataManager sharedManager].loginMember.userIcon placeHolder:@"loading_icon@2x.png"];
}

#pragma mark - Notification methods
- (void)iconChangedWithNotification:(NSNotification *)notification
{
    self.asynImageView.cacheDir = kUserImagesDir;
    [self.asynImageView aysnLoadImageWithUrl:[NYMemberCenterDataManager sharedManager].loginMember.userIcon placeHolder:@"loading_icon@2x.png"];
}

- (void)changeNameSucceedWithNotification:(NSNotification *)notification
{
    self.nameLabel.text = [NYMemberCenterDataManager sharedManager].loginMember.userName;
}


#pragma mark - UIView methods

- (void)awakeFromNib
{
    [super awakeFromNib];    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iconChangedWithNotification:) name:kIconChangedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNameSucceedWithNotification:) name:kChangeNameSucceedNotification object:nil];
    [self reloadData];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [asynImageView release];
    [nameLabel release];
    [huiyuanLabel release];
    [dengjiImageView release];
    [super dealloc];
}

- (IBAction)applyAuthButtonClicked:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowApplyAuthViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kHideMenuNotification object:nil];
}

- (IBAction)buttonPressed:(id)sender {
    
    if ([NYMemberCenterDataManager sharedManager].loginMember.memberType == kMemberAuthentication) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kShowUserGradeViewNotification object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:kHideMenuNotification object:nil];
    }
    else {
        if(![[NYMemberCenterDataManager sharedManager].loginMember.is_auth isEqualToString:@"1"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:kShowApplyAuthViewNotification object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:kHideMenuNotification object:nil];
        }
    }
}

- (IBAction)editButtonPressed:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowPersonalInfoViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kHideMenuNotification object:nil];
}

@end
